package com.nifostasky.comic.controller;

import com.nifostasky.comic.dtos.ForgetPasswordDTO;
import com.nifostasky.comic.dtos.LoginDTO;
import com.nifostasky.comic.dtos.RegisterDTO;
import com.nifostasky.comic.dtos.UserLoginDTO;
import com.nifostasky.comic.service.SysUserService;
import com.nifostasky.comic.utils.HttpRequestUtil;
import com.nifostasky.comic.utils.RandomValidateCodeUtil;
import com.nifostasky.comic.vos.JsonResult;
import com.nifostasky.comic.vos.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/login")
@Api(tags = "登录管理")
@Slf4j
@ApiSort(2)
@AllArgsConstructor
public class LoginController {

    private SysUserService userService;

    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @GetMapping("/getVerifyCode")
    @ApiOperationSupport(order = 1)
    public JsonResult getVerifyCode(HttpServletRequest request) {
        return userService.getVerifyCode(request);
    }

    @ApiOperation(value = "登录", notes = "用户登录,返回token等")
    @PostMapping("/login")
    @ApiOperationSupport(order = 2)
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request) {
        try {
            if(!StringUtils.hasText(userLoginDTO.getVerifyCode()))
                return new JsonResult(ResultCode.USER_CHECK_CODE_ERROR);
            if(1 != userLoginDTO.getLoginType() && 2 != userLoginDTO.getLoginType()){
                return new JsonResult(ResultCode.LOGIN_ERROR);
            }
            String ipAddress = HttpRequestUtil.getIpAddress(request);
            userLoginDTO.setIp(ipAddress);
            return userService.userLogin(userLoginDTO, request);
        } catch (Exception e) {
            log.error("登录异常:{}", e);
            return new JsonResult(ResultCode.LOGIN_ERROR);
        }
    }


    @ApiOperation(value = "用户退出登录", notes = "用户退出登录")
    @GetMapping("/loginOut")
    @ApiOperationSupport(order = 3)
    public JsonResult loginOut() {
        return userService.loginOut();
    }

    @ApiOperation(value = "用户登录 获取token(不需要输入verifyCode和Key)")
    @PostMapping("/getToken")
    @ApiOperationSupport(order = 4)
    public JsonResult userLogin(@RequestBody LoginDTO loginDTO, HttpServletRequest request) {
        try {
            UserLoginDTO userLoginDTO = new UserLoginDTO();
            JsonResult verifyCode = userService.getVerifyCode(request);
            Map<String,String> data = (Map<String, String>) verifyCode.getData();
            String key = data.get("key");
            userLoginDTO.setKey(key);
            String checkCode = String.valueOf(request.getSession().getAttribute(RandomValidateCodeUtil.RANDOMCODEKEY));
            userLoginDTO.setVerifyCode(checkCode);
            userLoginDTO.setEmail(loginDTO.getEmail());
            userLoginDTO.setPassWord(loginDTO.getPassWord());
            return userService.userLogin(userLoginDTO, request);
        } catch (Exception e) {
            log.error("登录异常:{}", e);
            return new JsonResult(ResultCode.LOGIN_ERROR);
        }
    }

    /**
     * 注册账号
     */
    @ApiOperation(value = "注册",notes = "注册账号")
    @PostMapping("/registerUser")
    @ApiOperationSupport(order = 5)
    public JsonResult registerUser(@RequestBody RegisterDTO registerDTO){
        return new JsonResult(userService.registerUser(registerDTO));
    }

    /**
     * 获取注册时的邮件验证码
     */
    @ApiOperation(value = "获取注册验证码",notes = "获取注册时的邮件验证码")
    @GetMapping("/generateEmailCode")
    @ApiOperationSupport(order = 6)
    public JsonResult generateEmailCode(String email){
        return new JsonResult(userService.generateEmailCode(email));
    }

    /**
     * 修改密码
     */
    @ApiOperation(value = "修改密码",notes = "修改密码")
    @PostMapping("/forgetPassword")
    @ApiOperationSupport(order = 7)
    public JsonResult forgetPassword(@RequestBody ForgetPasswordDTO forgetPasswordDTO) {
        return new JsonResult(userService.forgetPassword(forgetPasswordDTO));
    }
}
